package com.zhike.utils;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolUtils {
    private static ThreadPoolExecutor poolExecutor;

    public static ThreadPoolExecutor getThreadPoolExecutor() {
        // 第一次检查线程池是否为空
        if (poolExecutor == null) {
            // 对类进行同步以确保只有一个线程能创建线程池
            synchronized (ThreadPoolUtils.class) {
                // 第二次检查线程池是否为空（在进入同步块后再次检查）
                if (poolExecutor == null) {
                    // 创建并初始化线程池
                    poolExecutor = new ThreadPoolExecutor(
                            4, 20, 60 * 5,
                            TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>(Integer.MAX_VALUE));
                }
            }
        }
        // 返回已经初始化好的线程池实例
        return poolExecutor;
    }
}